

control_rows <-    list(
  "Controls" = c("", "X", "X"),
  "Controls interacted with worker is trans" = c("", "X", "")
)

# Setup ---------------------------------------------------------------
r2_choices_for_controls <- r2_choices %>%

  left_join(group_relations_ind, by = c("group_id", "ind_id")) %>%
  # Get interaction terms
  mutate(
    across(all_of(control_vars), list(pair_includes_trans = ~ .x * as.numeric(pair_includes_trans)))
  ) %>%
  # Get group level means
  group_by(group_id) %>%
  mutate(
    across(all_of(control_vars), list(group_control = ~ (sum_na(.x) - .x) / (n() - 1)))
  ) %>%
  ungroup %>%
  glimpse

# ............. ---------------------------------------------------------------
# MAIN RESULTS *************** ---------------------------------------------------------------

# Bar graph ---------------------------------------------------------------
df %>% group_by(discuss_type) %>% count_prop(video_type)

r2_summ <- r2_choices %>%
  filter(discuss_type %in% c("control", "discussion_full")) %>%
  group_by(discuss_type, pair_includes_trans) %>%
  summarise(mean_cl_boot(r2_choose_comparator),
            mean_cl_boot(r2_choose_comparator, conf.int = 0.9) %>% select(ymin_90 = ymin, ymax_90 = ymax),
            n_obs = n(),
            n_ind = n_distinct(ind_id),
            n_groups = n_distinct(group_id)
  ) %>%
  mutate(
    discuss_label = ifelse(discuss_type=="discussion_full", "3-person discussion", "No discussion (private)") %>%
      paste0(., "\n(N=", n_ind, ")") %>%
      fct_relevel(c(first(sort(unique(.))))),
    # discuss_type_label = fct_label_append(discuss_type, paste0(., "\n(N=", n_ind, ")")),
    pair_includes_trans_label = ifelse(pair_includes_trans, "Worker\nis trans", "Worker\nis non-trans") %>% paste0(., "\n(Obs=", n_obs, ")")
  )

main_bar <- r2_summ %>%
  ggplot(aes(x = pair_includes_trans_label, y = y, ymin = ymin, ymax = ymax, fill = pair_includes_trans)) +
  geom_col(show.legend = FALSE, position = position_dodge(0.8)) +
  geom_errorbar(width = 0.2, position = position_dodge(0.8)) +
  labs(y = "Probability of selecting worker in outcome round", fill = element_blank(), x = element_blank()) +
  scale_y_continuous(labels = scales::percent, expand = c(0, 0), limits = c(0, 0.65)) +
  geom_text(aes(y = 0.03, label = str_glue("{round(y, 3)*100}%")), position = position_dodge(0.8), size = 3) +
  facet_grid(~ discuss_label, scales = "free_x") +
  theme_minimal() +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.grid.major = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(),
    panel.spacing = unit(0.7, "cm")
  )

ggsave("outputs/figs/main_bar.pdf", width = 4.5, height = 4, scale = 1)

# Point graph ---------------------------------------------------------------

r2_n <- r2_choices %>%
  group_by(discuss_type, discussion_full, discuss, pair_includes_trans) %>%
  summarise(n_ind = n_distinct(ind_id),
            n_obs = n(),
            n_groups = n_distinct(group_id))

r2_choices_num %>%
  filter(discuss_type %in% c("control", "discussion_full")) %>%
  mutate(
    control_non_trans = !pair_includes_trans & discussion_full == 0,
    treat_non_trans = !pair_includes_trans & discussion_full == 1,
    control_trans = pair_includes_trans & discussion_full == 0,
    treat_trans = pair_includes_trans & discussion_full == 1,
  ) %>%
  mutate(across(c(control_non_trans, treat_non_trans, control_trans, treat_trans), as.numeric)) %>%

  feols_custom(
    r2_choose_comparator ~ (control_trans + treat_non_trans + treat_trans),
    data = .,
    fixef = c("stratum_id", "video_type", "comparator_order_in_pair"),
    cluster = "group_id",
    ri = FALSE
  ) %>%

  tidy_90 %>%

  filter(str_detect(term, "(control_trans|treat_non_trans|treat_trans)$")) %>%
  add_row(term = "omitted", estimate = 0, std.error = 0) %>%
  mutate(
    pair_includes_trans = str_detect(term, "control_trans|treat_trans"),
    discuss = str_detect(term, "treat")
  ) %>%

  left_join(r2_n %>% filter(discuss_type %in% c("control", "discussion_full"))) %>%
  mutate(
    discuss_label = ifelse(discuss, "After\n3-person discussion", "No discussion (private)") %>%
      fct_relevel("No discussion (private)") %>%
      fct_label_append(lab_append = paste0("\n(N=", n_ind, ")")),
    pair_includes_trans_label = ifelse(pair_includes_trans, "Worker\nis trans", "Worker\nis non-trans") %>% paste0(., "\n(Obs=", n_obs, ")")
  ) %>%

  ggplot(aes(x = pair_includes_trans_label, colour = pair_includes_trans)) +
  geom_hline(yintercept = 0, linetype = "dashed", colour = "skyblue") +
  geom_errorbar(aes(ymin = conf.low_90, ymax = conf.high_90), position = position_dodge(0.8), show.legend = F, width = 0, size = 1.2) +
  geom_errorbar(aes(ymin = conf.low, ymax = conf.high), position = position_dodge(0.8), show.legend = F, width = 0, size = 0.7) +
  geom_point(aes(y = estimate), position = position_dodge(0.8), show.legend = F, size = 3) +
  theme_minimal() +
  theme(panel.grid.major.y = element_blank(),
        panel.grid.minor.y = element_blank(),
        panel.grid.minor.x = element_blank(),
        panel.grid.major = element_blank()) +
  labs(x = element_blank(), y = "Effect on probability of selecting worker\n in outcome round") +
  facet_wrap(~ discuss_label, scales = "free_x")

ggsave("outputs/figs/main_graph_point.pdf", width = 4, height = 4)

ggplot2::ggsave("outputs/figs/main_graph_point.jpeg",
                device = "jpeg",
                width = 4, height = 4, dpi = 2000)

# Table ---------------------------------------------------------------

# r2_choices_num %>%
#   head() %>%
#   view()

cluster <- "group_id"

model_list <- list(
  "Chose worker in private\noutcome round (=1)" = feols_custom(
    r2_choose_comparator ~ pair_includes_trans * (discussion_full),
    data = r2_choices_num %>% filter(discuss_type %in% c("control", "discussion_full")),
    fixef = NULL,
    cluster = cluster,
    ri = ri,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    coef_keep = "^pair_includes_trans$|(^pair_includes_trans_alt\\:discussion_full$)|^discussion_full$|item_diff|r2_reliability_diff|r2_reliability_shown",
    coef_omit = "group_control|stratum_id|delivery_incentive_exp"
  ),
  "Chose worker in private\noutcome round (=1)" = feols_custom(
    r2_choose_comparator ~ pair_includes_trans_alt + pair_includes_trans_alt*(video_type_placebo + video_type_treatment + discussion_full + factor(stratum_id) + delivery_incentive_exp + phase) + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>% filter(discuss_type %in% c("control", "discussion_full")) %>% count_prop(phase),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair", "phase", "round"),
    cluster = cluster,
    ri = ri,
    n_sims = ri_sims,
    stratum = "stratum_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = "discussion_full",
      interact = "pair_includes_trans",
      group_control = TRUE
    ),
    var_types = var_types_spec,
    coef_keep = "^pair_includes_trans$|(^pair_includes_trans_alt\\:discussion_full$)|^discussion_full$|item_diff|r2_reliability_diff|r2_reliability_shown",
    coef_omit = "group_control|stratum_id|delivery_incentive_exp"
  ),
  "Chose trans in private\noutcome round (=1)\n(pairs with trans only)" = feols_custom(
    r2_choose_trans ~ video_type_placebo + video_type_treatment + discussion_full + factor(stratum_id) + delivery_incentive_exp + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>% filter(discuss_type %in% c("control", "discussion_full")) %>% filter(!is.na(r2_choose_trans)),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair", "phase"),
    cluster = cluster,
    ri = ri,
    n_sims = ri_sims,
    stratum = "stratum_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = "discussion_full",
      interact = "pair_includes_trans",
      group_control = TRUE
    ),
    var_types = var_types_spec,
    coef_keep = "^pair_includes_trans$|(^pair_includes_trans_alt\\:discussion_full$)|^discussion_full$|item_diff|r2_reliability_diff|r2_reliability_shown",
    coef_omit = "group_control|stratum_id|delivery_incentive_exp"
  )
)

r2_choices_num %>% count_prop(r2_choose_trans, r2_choose_comparator) %>%
  filter(r2_choose_trans != r2_choose_comparator) %>%
  count_prop(ind_id)

update_lasso_controls(model_list, table_name = "Effect of 3-person discussion\n(Table 1)")

tex_export(
  model_list,
  file = "outputs/tables/main_table.tex",
  coef_rename = coef_label,
  gof_map = fe_label_no_fe,
  coef_reorder = c("pair_includes_trans:discussion_full"),
  coef_omit = vars_to_regex(c("stratum_id", "Intercept", "video", "group_control", "pair_includes_trans_alt$", "phase", str_subset(control_vars, "r2_reliability|item", negate = TRUE), "delivery")),
  dep_means = list(
    "Mean: No discussion (private) + worker is non-trans" = "discuss_type == 'control' & pair_includes_trans == FALSE",
    "Mean: No discussion (private) + worker is trans" = "discuss_type == 'control' & pair_includes_trans == TRUE"
  ),
  stat_vec = "({std.error}) [{p.value}]",
  add_rows = list_to_add_rows(
    list(
      "Controls" = c("", "X", "X"),
      "Controls interacted with worker is trans" = c("", "X", "")
    )
  )
)

model_list[[2]] %>% tidy() %>% print_all

model_list[[2]]$collin.coef

# OUTPUT STATS:
(model_list[[1]]$coefficients["pair_includes_trans"] * -100) %>% write_stat("outputs/stats/baseline_discrim.tex", digits = 0)
(model_list[[1]]$coefficients["pair_includes_trans"] * -100) %>% write_stat("outputs/stats/baseline_discrim_round.tex", digits = 0)
(model_list[[1]]$coefficients["pair_includes_trans:discussion_full"] * 100) %>% write_stat("outputs/stats/main_treatment_effect.tex", digits = 0)

# Percentages
((model_list[[1]] %>% get_coeff("pair_includes_trans") * -1) / (model_list[[1]] %>% get_coeff("(Intercept)"))) %>%
  write_percentage("outputs/stats/baseline_discrim_perc.tex")

(get_coeff(model_list[[1]], "pair_includes_trans:discussion_full") / (get_coeff(model_list[[1]], "(Intercept)") + get_coeff(model_list[[1]], "pair_includes_trans"))) %>%
  write_percentage("outputs/stats/main_treatment_effect_perc.tex")

# Item sensitivity
((model_list[[2]] %>% get_coeff("item_diff")) * 100 ) %>% write_stat("outputs/stats/item_sensitivity.tex", digits = 0)

# Score sneisity
((model_list[[2]] %>% get_coeff("r2_reliability_diff")) * 100 ) %>% write_stat("outputs/stats/reliability_sensitivity.tex", digits = 0)

# Get p-value for discrimination level in the treatment group
feols_custom(
  r2_choose_comparator ~ pair_includes_trans * (control),
  data = r2_choices_num %>% filter(discuss_type %in% c("control", "discussion_full")),
  fixef = NULL,
  cluster = "group_id",
  ri = ri,
  n_sims = ri_sims,
  stratum = "stratum_id",
  var_types = var_types_spec,
  coef_keep = "^pair_includes_trans$|(^pair_includes_trans\\:discussion_full$)|^discussion_full$|item_diff|r2_reliability_diff|r2_reliability_shown",
  coef_omit = "group_control|stratum_id|delivery_incentive_exp"
) %>%
  tidy_90() %>%
  filter(term == "pair_includes_trans") %>%
  pull(p.value) %>%
  write_stat("outputs/stats/treatment_discrim_p.tex", digits = 2)

# Phase 2 - bar ---------------------------------------------------------------

r2_choices %>%
  filter(phase == "phase_2") %>%
  group_by(treat_type_r2, treat_type_r2_label, pair_includes_trans) %>%
  summarise(mean_cl_boot(r2_choose_comparator),
            n_obs = n(),
            n_ind = n_distinct(ind_id),
            n_groups = n_distinct(group_id)
  ) %>%

  mutate(
    treat_type_r2_label = treat_type_r2_label %>% fct_label_append(lab_append = paste0("\n(N=", n_ind, ")")),
    pair_includes_trans_label = ifelse(pair_includes_trans, "Worker\nis trans", "Worker\nis non-trans") %>% paste0(., "\n(Obs=", n_obs, ")")
  ) %>%

  ggplot(aes(x = pair_includes_trans_label, y = y, ymin = ymin, ymax = ymax, fill = pair_includes_trans)) +
  geom_col(show.legend = FALSE, position = position_dodge(0.8)) +
  geom_errorbar(width = 0.2, position = position_dodge(0.8)) +
  labs(y = "Probability of selecting the worker", fill = element_blank(), x = element_blank()) +
  scale_y_continuous(labels = scales::percent, expand = c(0, 0)) +
  geom_text(aes(y = 0.03, label = str_glue("{round(y, 3)*100}%")), position = position_dodge(0.8), size = 3) +
  facet_grid(~ treat_type_r2_label, scales = "free_x") +
  theme_minimal() +
  theme(
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    panel.grid.minor.x = element_blank(),
    panel.grid.major = element_blank(),
    axis.line = element_line(),
    axis.ticks = element_line(),
    panel.spacing = unit(0.7, "cm")
  )

ggsave("outputs/figs/main_bar_phase_2.pdf", width = 15, height = 7)


# Phase 2 - table ---------------------------------------------------------------

set.seed(12345)

models_phase_2 <- list(
  "Chose worker in private\noutcome round (=1)" = feols_custom(
    r2_choose_comparator ~ pair_includes_trans * (public_observer + public_non_observer + discussion_pair_speaker + discussion_pair_listener + discussion_full),
    data = r2_choices_num %>% filter(phase == "phase_2"),
    fixef = NULL,
    cluster = "group_id",
    ri = ri,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    # coef_omit = "stratum_id|Intercept|video",
    coef_keep = vars_to_regex(c("^pair_includes_trans$", "public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full")),
    control_group = list(
      "control" = c("public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full")
    ),
    same_group_spec = list(
      "public" = c("public_non_observer", "public_observer"),
      "discussion_pair" = c("discussion_pair_speaker", "discussion_pair_listener")
    )
  ),
  "Chose worker in private\noutcome round (=1)" = feols_custom(
    r2_choose_comparator ~ pair_includes_trans_alt + pair_includes_trans_alt*(video_type_placebo + video_type_treatment + public_observer + public_non_observer + discussion_pair_speaker + discussion_pair_listener + discussion_full + factor(stratum_id)) + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>% filter(phase == "phase_2"),
    fixef = c("stratum_id", "video_type", "comparator_order_in_pair"),
    cluster = "group_id",
    ri = ri,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full"),
      interact = "pair_includes_trans",
      group_control = TRUE
    ),
    coef_keep = vars_to_regex(c("^pair_includes_trans$", "public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full")),
    control_group = list(
      "control" = c("public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full")
    ),
    same_group_spec = list(
      "public" = c("public_non_observer", "public_observer"),
      "discussion_pair" = c("discussion_pair_speaker", "discussion_pair_listener")
    )
  ),
  "Chose trans in private\noutcome round (=1)\n(pairs with trans only)" = feols_custom(
    r2_choose_trans ~ (pair_includes_trans) * (video_type_placebo + video_type_treatment + public_observer + public_non_observer + discussion_pair_speaker + discussion_pair_listener + discussion_full + factor(stratum_id)) + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>% filter(phase == "phase_2") %>% filter(!is.na(r2_choose_trans)),
    fixef = c("stratum_id", "video_type", "comparator_order_in_pair"),
    cluster = "group_id",
    ri = ri,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full"),
      interact = "pair_includes_trans",
      group_control = TRUE
    ),
    coef_keep = vars_to_regex(c("^pair_includes_trans$", "public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full")),
    control_group = list(
      "control" = c("public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full")
    ),
    same_group_spec = list(
      "public" = c("public_non_observer", "public_observer"),
      "discussion_pair" = c("discussion_pair_speaker", "discussion_pair_listener")
    )
  )
)
  
phase_2_r2_vars <- c(
  "control",
  "public_observer",
  "public_non_observer",
  "discussion_pair_speaker",
  "discussion_pair_listener",
  "discussion_full"
)

p_vals_phase_2 <- models_phase_2 %>%
  map(
    ~ get_comparison_p_vals(.x,
                            dummy_vars = phase_2_r2_vars,
                            ri = FALSE,
                            control_group = list("control" = c("public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full")),
                            same_group_spec = list(
                              "public" = c("public_non_observer", "public_observer"),
                              "discussion_pair" = c("discussion_pair_speaker", "discussion_pair_listener")
                            ),
                            n_sims = ri_sims,
                            lasso = FALSE,
                            stratum = "stratum_id", var_types = var_types_spec,
                            coef_keep = vars_to_regex(phase_2_r2_vars))
  ) %>%
  purrr::map(filter_interact_terms, vars = phase_2_r2_vars, interact = "pair_includes_trans(_alt)?") %>%
  map(filter, paste0(base, "_", term) %in% c(
    "public_observer_public_non_observer",
    "public_observer_discussion_pair_speaker",
    "public_observer_discussion_pair_listener",
    "public_observer_discussion_full",
    "discussion_pair_speaker_discussion_pair_listener",
    "discussion_pair_speaker_discussion_full",
    "discussion_pair_listener_discussion_full"
  )) %>%
  purrr::map(~ mutate(.x, across(c(base, term), ~ .x %>% coef_label %>% str_replace_all("\\s\\(.*\\)", ""))))

tex_export(
  models_phase_2,
  file = "outputs/tables/main_phase_2.tex",
  coef_rename = coef_label,
  gof_map = fe_label_no_fe,
  coef_reorder = c(
    "pair_includes_trans:discussion_full",
    "pair_includes_trans:discussion_pair_speaker",
    "pair_includes_trans:discussion_pair_listener",
    "pair_includes_trans:public_observer",
    "pair_includes_trans:public_non_observer",
    "discussion_full",
    "discussion_pair_speaker",
    "discussion_pair_listener",
    "public_observer",
    "public_non_observer",
    "pair_includes_trans"
  ),
  stat_vec = "({std.error}) [{p.value}]",
  coef_omit = vars_to_regex(c("stratum_id", "Intercept", "video", "group_control", "phase", "pair_includes_trans_alt$", control_vars, "delivery")),
  dep_means = list(
    "Mean: No discussion (private) + worker is non-trans" = "discuss_type == 'control' & pair_includes_trans == FALSE",
    "Mean: No discussion (private) + worker is trans" = "discuss_type == 'control' & pair_includes_trans == TRUE"
  ),
  add_rows = combine_add_rows(
    list_to_add_rows(control_rows),
    add_rows = p_vals_to_add_rows(p_vals_phase_2)
  )
)

# Effect of listener
(models_phase_2[[1]]$coefficients["pair_includes_trans:discussion_pair_listener"] * 100) %>%
  write_stat("outputs/stats/effect_listener.tex", digits = 1)

# Effect of 3-person discussion
(models_phase_2[[1]]$coefficients["pair_includes_trans:discussion_full"] * 100) %>%
  write_stat("outputs/stats/effect_discussion_full_phase_2.tex", digits = 1)

# Effect of 2-person discussion
(models_phase_2[[1]]$coefficients["pair_includes_trans:discussion_pair_speaker"] * 100) %>%
  write_stat("outputs/stats/effect_speaker.tex", digits = 1)

models_phase_2[[2]] %>% tidy_90 %>% print_all

c(
  models_phase_2[[1]] %>% get_p_val("pair_includes_trans:public_non_observer"),
  models_phase_2[[2]] %>% get_p_val("pair_includes_trans_alt:public_non_observer"),
  models_phase_2[[3]] %>% get_p_val("public_non_observer")
) %>%
  write_range("outputs/stats/p_vals_public_non_observer.tex", digits = 2)

c(
  models_phase_2[[1]] %>% get_p_val("pair_includes_trans:public_observer"),
  models_phase_2[[2]] %>% get_p_val("pair_includes_trans_alt:public_observer"),
  models_phase_2[[3]] %>% get_p_val("public_observer")
) %>%
  write_range("outputs/stats/p_vals_public_observer.tex", digits = 2)

models_phase_2[[1]] %>% get_coeff("pair_includes_trans:public_observer") %>%
  times_100() %>%
  write_stat("outputs/stats/effect_public_observer.tex", digits = 1)

# p-value listener vs speaker
p_vals_phase_2[[1]] %>% filter(str_detect(base, "Speaker"), str_detect(term, "Listener")) %>%
  pull(p.value) %>%
  write_stat("outputs/stats/p_val_listener_speaker.tex", digits = 2)

# p-value listener vs full
p_vals_phase_2[[1]] %>% filter(str_detect(base, "Listener"), str_detect(term, "3-person")) %>%
  pull(p.value) %>%
  write_stat("outputs/stats/p_val_listener_full_discussion.tex", digits = 2)

# p-value speaker vs full
p_vals_phase_2[[1]] %>% filter(str_detect(base, "Speaker"), str_detect(term, "3-person")) %>%
  pull(p.value) %>%
  write_stat("outputs/stats/p_val_speaker_full_discussion.tex", digits = 2)

# p-value - observer vs non-observer
p_vals_phase_2[[1]] %>% filter(str_detect(base, "Observer"), str_detect(term, "Non-observer")) %>%
  pull(p.value) %>%
  write_stat("outputs/stats/p_val_observer_non_observer.tex", digits = 2)

# R2 ----------------------------------------------------------------------------------------------------------------

feols_custom(
  r2_choose_comparator ~ pair_includes_trans * (discussion_full),
  data = r2_choices_num %>% filter(discuss_type %in% c("control", "discussion_full")),
  fixef = NULL,
  cluster = "group_id",
  ri = ri,
  n_sims = ri_sims,
  stratum = "stratum_id",
  var_types = var_types_spec,
  coef_keep = "^pair_includes_trans$|(^pair_includes_trans_alt\\:discussion_full$)|^discussion_full$|item_diff|r2_reliability_diff|r2_reliability_shown",
  coef_omit = "group_control|stratum_id|delivery_incentive_exp"
)

feols_custom(
  r2_choose_comparator ~ (discussion_full),
  data = r2_choices_num %>% filter(discuss_type %in% c("control", "discussion_full"), pair_includes_trans == 1),
  fixef = NULL,
  cluster = "group_id",
  ri = ri,
  n_sims = ri_sims,
  stratum = "stratum_id",
  var_types = var_types_spec,
  coef_keep = "^pair_includes_trans$|(^pair_includes_trans_alt\\:discussion_full$)|^discussion_full$|item_diff|r2_reliability_diff|r2_reliability_shown",
  coef_omit = "group_control|stratum_id|delivery_incentive_exp"
)

# Persuasion rate ----------------------------------------------------------------------------------------------------------------

model_no_frills <- feols_custom(
  r2_choose_trans ~ discussion_full ,
  data = r2_choices_num %>% filter(discuss_type %in% c("control", "discussion_full")) %>% filter(!is.na(r2_choose_trans)),
  cluster = "group_id"
)

y_control <- model_no_frills %>% get_coeff("(Intercept)")
y_treat <- (model_no_frills %>% get_coeff("discussion_full")) + y_control

pers_rate <- (y_treat - y_control) / (1 - y_control)

pers_rate %>% write_percentage("outputs/stats/persuasion_rate.tex", digits = 0)

# Cobined mechanism treatments (treatment and outcome round) ----------------------------------------------------------------------------------------------------------------
set.seed(12345)
models_mechs_combined <- list(
  "Treatment round" = feols_custom(
    r1_choose_comparator ~ video_type + public_pooled + discussion_pair_speaker + discussion_full + factor(stratum_id) + item_diff + quality_diff * quality_included,
    data = r1_choices %>%
      filter(phase == "phase_2", !is.na(r1_choose_trans)) %>% count_prop(public, discussant) %>%
      mutate(public_pooled = public),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id",
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("public_pooled", "discussion_pair_speaker", "discussion_full"),
      interact = "pair_includes_trans",
      group_control = FALSE
    ),
    ri = ri,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    control_group = list(
      "control" = c("public_pooled", "discussion_pair_speaker", "discussion_full")
    ),
    same_group_spec = list(
      "discussion_pair" = c("discussion_pair_speaker", "discussion_pair_listener")
    ),
    coef_omit = "stratum_id|Intercept|video|quality|item"
  ),
  "Outcome round" = feols_custom(
    r2_choose_trans ~ (pair_includes_trans) * (video_type_placebo + video_type_treatment + public_observer + public_non_observer + discussion_pair_speaker + discussion_pair_listener + discussion_full + factor(stratum_id)) + item_diff + r2_reliability_diff * r2_reliability_shown ,
    data = r2_choices_num %>% filter(phase == "phase_2") %>% filter(!is.na(r2_choose_trans)),
    fixef = c("stratum_id", "video_type", "delivery_incentive_exp", "comparator_order_in_pair"),
    cluster = "group_id",
    ri = ri,
    n_sims = ri_sims,
    stratum = "stratum_id",
    var_types = var_types_spec,
    lasso = TRUE,
    lasso_options = list(
      potential_controls = control_vars,
      t = c("public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full"),
      interact = "pair_includes_trans",
      group_control = TRUE
    ),
    coef_keep = vars_to_regex(c("public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full")),
    control_group = list(
      "control" = c("public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full")
    ),
    same_group_spec = list(
      "public" = c("public_non_observer", "public_observer"),
      "discussion_pair" = c("discussion_pair_speaker", "discussion_pair_listener")
    )
  )
)

p_vals_mechs_r1 <- models_mechs_combined[1] %>%
  purrr::map(
    get_comparison_p_vals, dummy_vars = c("control", "public_pooled", "discussion_pair_speaker", "discussion_full"),
    ri = TRUE,
    n_sims = ri_sims, stratum = "stratum_id", var_types = var_types_spec,
    coef_keep = "control|public_pooled|discuss",
    control_group = list(
      "control" = c("public_pooled", "discussion_pair_speaker", "discussion_full")
    ),
    same_group_spec = list(
      "discussion_pair" = c("discussion_pair_speaker", "discussion_pair_listener")
    )
  ) %>%
  purrr::map(filter_interact_terms, vars = c("control", "public_pooled", "discussion_pair_speaker", "discussion_full"), interact = "pair_includes_trans(_alt)?") %>%
  map(filter, row_number() %in% c(2, 3, 6)) %>%
  map(~ .x %>% mutate(across(c(base, term), ~.x %>% coef_label %>% str_replace("\\s\\(2-person.*\\)", "") %>%
    str_replace("public_pooled|No discussion \\(public\\), pooled", "Public"))))


phase_2_r2_vars <- c(
  "control",
  "public_observer",
  "public_non_observer",
  "discussion_pair_speaker",
  "discussion_pair_listener",
  "discussion_full"
)

p_vals_mechs_r2 <- models_mechs_combined[2] %>%
  map(
    ~ get_comparison_p_vals(.x,
                            dummy_vars = phase_2_r2_vars,
                            ri = TRUE,
                            control_group = list("control" = c("public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full")),
                            same_group_spec = list(
                              "public" = c("public_non_observer", "public_observer"),
                              "discussion_pair" = c("discussion_pair_speaker", "discussion_pair_listener")
                            ),
                            n_sims = ri_sims,
                            stratum = "stratum_id", var_types = var_types_spec,
                            coef_keep = paste0("^(", vars_to_regex(phase_2_r2_vars), ")$"))
  ) %>%
  purrr::map(filter_interact_terms, vars = phase_2_r2_vars, interact = "pair_includes_trans(_alt)?") %>%
  map(filter, paste0(base, "_", term) %in% c(
    "public_observer_public_non_observer",
    "public_observer_discussion_pair_speaker",
    "public_observer_discussion_pair_listener",
    "public_observer_discussion_full",
    "discussion_pair_speaker_discussion_pair_listener",
    "discussion_pair_speaker_discussion_full",
    "discussion_pair_listener_discussion_full"
  )) %>%
  purrr::map(~ mutate(.x, across(c(base, term), ~ .x %>% coef_label %>% str_replace_all("\\s\\(.*\\)", ""))))

# p_vals_mechs_r1

p_vals_mechs_combined <- full_join(
  p_vals_mechs_r1[[1]],
  p_vals_mechs_r2[[1]],
  by = c("base", "term")
) %>%
  mutate(
    label = str_glue("p({base}={term})"),
  ) %>%
  relocate(label) %>%
  select(-base, -term)

tex_export(
  models_mechs_combined,
  file = "outputs/tables/mechs_combine_r1_r2.tex",
  coef_rename = coef_label,
  gof_map = fe_label_no_fe,
  coef_omit = vars_to_regex("stratum_id|Intercept|video_type|dq12|pair_includes_trans_alt$|item|reliability|quality", control_vars_except_items_reliability),
  dep_means = list(
    "Mean: No discussion (private) + worker is non-trans" = "discuss_type == 'control' & pair_includes_trans == FALSE",
    "Mean: No discussion (private) + worker is T" = "discuss_type == 'control' & pair_includes_trans == TRUE"
  ),
  stat_vec = "({std.error}) [{p.value}]",
  additional_header = vec_to_custom_header(c(
    " ", rep("Dep var: Chose trans (=1)\n(pairs with trans only)", 2)
  )),
  coef_reorder = c("public_pooled", "public_observer", "public_non_observer", "discussion_pair_speaker", "discussion_pair_listener", "discussion_full"),
  add_rows = combine_add_rows(
    p_vals_mechs_combined
  )
)

models_mechs_combined[[1]] %>%
  tidy() %>%
  filter(term == "public_pooled") %>%
  pull(p.value) %>%
  write_stat("outputs/stats/p_val_r1_public.tex", digits = 2)

models_mechs_combined[[1]] %>%
  get_coeff("public_pooled") %>%
  times_100() %>% 
  write_stat("outputs/stats/effect_r1_public.tex", digits = 1)
